﻿using System;
using System.Collections;
using System.Xml;
using System.Text;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker
{
	
	class XSNManifestRuleButtonRuleSetParseWorker
	{
		//遍历所有视图中的RuleButton,并解析对应RuleSet,如果没有找到对应RuleSet,则把RuleButton从列表中除去.
		public static void parseRuleButtonRuleSet(Manifest manifest)
		{
			foreach( View view in manifest.ViewList )
			{
				if( view.RuleButtonList != null )
				{
					ArrayList removeList = new ArrayList();
					foreach( RuleButton ruleButton in view.RuleButtonList )
					{
						RuleSet ruleSet = XSNManifestRuleSetParseWorker.parseRuleSetByName( manifest, ruleButton.RuleSetName );
						//CID 0261 //2009.03.25 //Begin
                        //CID 0735 //2010.12.19 //Begin
						ruleSet = InfoJetSoft.Service.EscapeRule.RuleSetFilter.Filter( manifest, view, ruleButton.ButtonName, ruleSet );
                        //CID 0735 //2010.12.19 //End
						//CID 0261 //2009.03.25 //End
						if( ruleSet != null )
						{
							ruleButton.RuleSet = ruleSet;
							if( ruleButton.HtmlButtonList != null )
							{
								//判断按钮规则中是否要提交表单,如果提交表单则设置HtmlButton的Submit_Button属性为true.
								foreach( Rule rule in ruleButton.RuleSet.RuleList )
								{
									if( !rule.Enabled )
									{
										continue;
									}
									foreach( RuleAction ruleAction in rule.RuleActionList )
									{
										if( ruleAction.RuleActionType == RuleAction.iSubmitAction )
										{
											//将所有HtmlButton的Submit_Button属性为true.
											foreach( XmlElement inputElement in ruleButton.HtmlButtonList )
											{
												//在infojet.js中如果HTML_SUBMIT_BUTTON为true,那么只有在表单页面中无错误才提交.
												//这样一个限制就是,即使在前面的RuleAction修正了表单中的错误,因为在客户端无法提交,也不能执行.
												//但是,似乎这种在RuleAction中自动修正表单错误不太合理.
												//如果确定不能用HTML_SUBMIT_BUTTON在客户端作,那么就需要在提交前进行验证.
												inputElement.SetAttribute( JetConstant.HTML_SUBMIT_BUTTON, JetConstant.HTML_true );
											}
											break;
										}
									}
								}
							}
						}
						else
						{
							removeList.Add( ruleButton );
						}
					}
					foreach( RuleButton ruleButton in removeList )
					{
						view.RuleButtonList.Remove( ruleButton );
					}
				}
			}
		}

	}
}